package com.olap.starter.core.response;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.olap.starter.core.response.paging.TablePageResponse;

import java.util.List;

/**
 * @author WBG
 * @date 2021/9/5 22:00
 * @describe
 */
public class ResultUtil {
    public static <T> Result<T> success() {
        return success(null, null);
    }

    public static <T> Result<T> success(String msg) {
        return success(null, msg);
    }

    public static <T> Result<T> success(T data) {
        return success(data, null);
    }

    public static <T> Result<T> success(T data, String msg) {
        return new Result<>(200, msg, data);
    }

    public static <T> Result<T> error() {
        return error(null, null);
    }

    public static <T> Result<T> error(String msg) {
        return error(null, msg);
    }

    public static <T> Result<T> error(T data) {
        return error(data, null);
    }

    public static <T> Result<T> error(T data, String msg) {
        return new Result<>(500, msg, data);
    }

    public static <T> Result<T> error(int code, String msg) {
        return new Result<>(code, msg);
    }

    public static <T> Result<T> addStatusMsg(boolean statusCode) {
        return new Result<>(autoHttpResponseStatus(statusCode), autoMessage(Increment.ADD, statusCode));
    }

    public static <T> Result<T> modifyStatusMsg(boolean statusCode) {
        return new Result<>(autoHttpResponseStatus(statusCode), autoMessage(Increment.UPDATE, statusCode));
    }

    public static <T> Result<T> removeStatusMsg(boolean statusCode) {
        return new Result<>(autoHttpResponseStatus(statusCode), autoMessage(Increment.DELETE, statusCode));
    }

    public static <T> Result<T> defaultStatusMsg(boolean statusCode) {
        return new Result<>(autoHttpResponseStatus(statusCode), autoMessage(Increment.DEFAULT, statusCode));
    }

    public static int autoHttpResponseStatus(boolean statusCode) {
        return statusCode ? 200 : 500;
    }

    public static String autoMessage(Increment increment, boolean statusCode) {
        if (increment == Increment.ADD) {
            return statusCode ? "保存成功！" : "保存失败！";
        } else if (increment == Increment.UPDATE) {
            return statusCode ? "更新成功！" : "保存失败！";
        } else if (increment == Increment.DELETE) {
            return statusCode ? "删除成功！" : "删除失败！";
        } else {
            return statusCode ? "操作成功！" : "操作失败！";
        }
    }

    public enum Increment {
        ADD, UPDATE, DELETE, DEFAULT
    }

    /**
     * 响应请求分页数据
     */
    public static TablePageResponse respTablePageResponse(List<?> list) {
        TablePageResponse rspData = new TablePageResponse();
        rspData.setCode(200);
        rspData.setData(list);
        rspData.setMsg("查询成功！");
        return rspData;
    }
    public static TablePageResponse respTablePageResponse(IPage iPage) {
        TablePageResponse rspData = new TablePageResponse();
        rspData.setCode(200);
        rspData.setData(iPage.getRecords());
        if(iPage.getTotal() ==0 && iPage.getRecords().size() !=0){
            rspData.setTotal(iPage.getRecords().size());
        }else {
            rspData.setTotal(iPage.getTotal());
        }
        rspData.setMsg("查询成功！");
        return rspData;
    }

    /**
     * 响应请求分页数据
     */
    public static TablePageResponse respTablePageResponse(List<?> list, int total) {
        TablePageResponse rspData = new TablePageResponse();
        rspData.setCode(200);
        rspData.setData(list);
        rspData.setMsg("查询成功！");
        rspData.setTotal(total);
        return rspData;
    }

}
